<template>
	<view class="container">
		<view class="formBox">
			<uni-forms ref="form" :modelValue="FormData" :rules="rules">
				<view class="box">
					<uni-forms-item required label="分组" name="group_id">
						<picker @change="bindGroup" :range="group" range-key="group_name">
							<view class="picker-input">
								{{ group_name }}
								<uni-icons type="right" size="16" color="#bbb"></uni-icons>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item required label="门牌号" name="home_name">
						<uni-easyinput type="text" v-model="FormData.home_name" :clearable="false" placeholder="请输入门牌号" />
					</uni-forms-item>
				</view>
				<view class="btns">
					<button class="btn" type="primary" @click="submit">确 定</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
import api from '@/api/api';
export default {
	data() {
		return {
			group_name: '',
			FormData: {},
			group: [],
			rules: {
				home_name: {
					rules: [
						{
							required: true,
							errorMessage: '请填写门牌号'
						}
					],
					validateTrigger: 'submit'
				}
			}
		};
	},
	onLoad(e) {
		this.FormData = e;
	},
	onShow() {
		this.getGroup();
	},
	methods: {
		bindGroup(e) {
			this.FormData.group_id = this.group[e.detail.value].id;
			this.group_name = this.group[e.detail.value].group_name;
		},
		submit() {
			this.$refs.form.validate().then((res) => {
				api.homeEdit(this.FormData).then((res) => {
					if (res.data.is_success) {
						uni.showToast({
							title: '修改成功'
						});
						setTimeout(() => {
							uni.hideToast();
							uni.navigateBack();
						}, 1000);
					} else {
						uni.showToast({
							title: '无内容更改',
							icon: 'none'
						});
					}
				});
			});
		},
		getGroup() {
			api.groupList({}).then((data) => {
				this.group = data.data;
				for (let i = 0; i < this.group.length; i++) {
					if (this.FormData.group_id == this.group[i].id) {
						this.group_name = this.group[i].group_name;
					}
				}
			});
		}
	}
};
</script>